package org.ichihamigua.common.qiniucloud.service.impl;

import com.alibaba.fastjson2.JSONObject;
import org.ichihamigua.common.framework.entity.Result;
import org.ichihamigua.common.qiniucloud.vo.InitPartUploadResponseVo;
import org.ichihamigua.common.qiniucloud.vo.UploadFileVo;
import org.ichihamigua.common.qiniucloud.vo.UploadPartListResponseVo;
import org.ichihamigua.common.qiniucloud.vo.UploadPartVo;
import org.ichihamigua.common.qiniucloud.service.QiniuObjectStoreService;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * @author rainkyzhong
 * @date 2023/10/29 1:14
 */
@Service
public class QiniuObjectStoreServiceImpl extends QiniuRequestServiceImpl implements QiniuObjectStoreService{

    //七牛云对象存储对接文档: https://developer.qiniu.com/kodo/1274/rs

    @Override
    public Result<UploadFileVo> upload(String bucketName, String fileName, MultipartFile file) {
        return Result.success(new UploadFileVo(bucketName, fileName));
    }

    @Override
    public Result<JSONObject> delete(String encodedEntryURI) {
        return null;
    }

    @Override
    public Result<InitPartUploadResponseVo> initiateMultipartUpload(String bucketName, String saveName) {
        return request(
                String.format(
                        "/buckets/%s/objects/%s/uploads",
                        bucketName,
                        new String(Base64.getUrlEncoder().encode(saveName.getBytes(StandardCharsets.UTF_8)))
                ),
                bucketName,
                HttpMethod.POST,
                null,
                null,
                InitPartUploadResponseVo.class

        );
    }

    @Override
    public Result<UploadPartVo> uploadPart(String bucketName, String saveName, String uploadId, Integer partNumber, MultipartFile file){
        return uploadPart(
                bucketName,uploadId,partNumber,saveName,file
        );
    }

    /**
     * 获取已上传成功的分片
     * @param bucketName 空间名
     * @param saveName 保存名
     * @param uploadId 上传任务ID
     * @return UploadPartListResponseVo
     */
    private UploadPartListResponseVo listUploadPart(
            String bucketName,
            String saveName,
            String uploadId
    ){
        Result<UploadPartListResponseVo> res = request(
                String.format(
                        "/buckets/%s/objects/%s/uploads/%s?max-parts=%d",
                        bucketName,
                        new String(Base64.getUrlEncoder().encode(saveName.getBytes(StandardCharsets.UTF_8))),
                        uploadId,
                        1000
                ),
                bucketName,
                HttpMethod.GET,
                null,
                null,
                UploadPartListResponseVo.class
        );
        if(!res.getSuccess() || res.getData().getParts() == null){
            return null;
        }
        return res.getData();
    }

    @Override
    public Result<JSONObject> completeMultipartUpload(
            String bucketName,
            String saveName,
            String uploadId
    ){
        UploadPartListResponseVo listUploadPart = listUploadPart(bucketName, saveName, uploadId);
        if(listUploadPart == null || listUploadPart.getParts() == null)  return Result.fail("上传任务已失效");
        return request(
                String.format(
                        "/buckets/%s/objects/%s/uploads/%s",
                        bucketName,
                        new String(Base64.getUrlEncoder().encode(saveName.getBytes(StandardCharsets.UTF_8))),
                        uploadId
                ),
                bucketName,
                HttpMethod.POST,
                JSONObject.of("parts", listUploadPart.getParts()),
                null,
                JSONObject.class
        );
    }
}
